#!/usr/bin/env python
# encoding: utf-8
"""
@summary: 处理hadoop上的文件
@author: hongxingfan
@since: 2014年8月5日    下午7:03:23
"""
from os.path import os
import MySQLdb

class Deal:
    
    def __init__(self, host, uname, passwd, dbname):
        self.host = host
        self.uname = uname
        self.passwd = passwd
        self.dbname = dbname
        self.cityCodeToInfo = {}
    
    # 将数据装载到hash结构中
    def DBToHash(self):
        try:
            conn = MySQLdb.connect(host = self.host, user = self.uname, passwd = self.passwd, db = self.dbname, charset = "utf8") 
            cursor = conn.cursor()
            sql = "SELECT city_code,city_name,basic_province.province_name FROM basic_city LEFT JOIN basic_province ON basic_province.province_code = basic_city.province_code"
            cursor.execute(sql)
            for (city_code, city_name, province_name) in cursor.fetchall():
                self.cityCodeToInfo[city_code] = city_name + "\t" + province_name
        except MySQLdb.Error, e:
            print("Error Code is:%s; Error Message is:%s" % (e.args[0], e.args[1]))
        finally:
            cursor.close()
            conn.close()
    
    # 从hdfs上拉取文件,并转化成中文
    def getDataFromHDFS(self, yesterday):
        l_list = []
        totalPv = 0
        totalUv = 0
        try:
            hdfs = os.popen("hadoop dfs -text /user/autolog/tmp/fan/DistributeOfCountry_" + yesterday + "/part*")
            # hdfs = os.popen("hadoop dfs -text /user/autolog/tmp/fan/002/part-00000")
            for line in hdfs.readlines():
                line = line.strip()
                cols = line.split("\t")
                region = self.cityCodeToInfo.get(cols[0], cols[0] + "\t")
                pv = int(cols[1])
                uv = int(cols[2])
                totalPv += pv
                totalUv += uv
                entity = Entity(region.encode("utf-8"), pv, uv)  # 解决乱码问题
                l_list.append(entity)
        except:
            print("hdfs command has error...")
        finally:
            hdfs.close()
        l_list.sort(lambda x, y: cmp(int(y.pv), int(x.pv)))  # 列表排序
        l_list.insert(0, Entity("totalPV\ttotalUv", totalPv, totalUv))
        return l_list

# 实体类    
class Entity(object):
    def __init__(self, region, pv, uv):
        self.region = region
        self.pv = pv
        self.uv = uv
    def __unicode__(self):
        print("%s\t%s\t%s" % (self.region, self.pv, self.uv))    

# 测试代码
# deal = Deal(host = "10.11.157.107", uname = "db_datacenter", passwd = "11111111", dbname = "db_datacenter")
# deal.DBToHash()
# for entity in deal.getDataFromHDFS():
#     print("%s\t%s\t%s" % (entity.region, entity.pv, enipv)) 
